/**
 * @Project POL3Cay
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 23, 2011
 * @Createtime 11:44:50 AM
 */

package test
{
	import assets.CardRender;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;

	import models.card.BaseHand;
	import models.card.Card;

	public class DrawHand extends Sprite
	{
		protected var hand:BaseHand;

		private var _openAngle:int = 0;

		private var _radius:int = 0;

		protected var numCards:int = 0;

		private var cardsSetRender:Vector.<CardRender>;

		public function DrawHand()
		{
			super();

			hand = new BaseHand();
			cardsSetRender = new Vector.<CardRender>();

			for (var i:int = 0; i < 52; i++)
			{
				cardsSetRender[i] = null;
			}
		}

		public function set handCards(cardsSet:Vector.<Card>):void
		{
			numCards = 0;
			var card:Card;

			for each (card in cardsSet)
			{
				if (cardsSetRender[numCards] == null)
				{
					cardsSetRender[numCards] = new CardRender();
					addChild(cardsSetRender[numCards]);
				}

				cardsSetRender[numCards].setCard(card.value, card.suit);

				numCards++;
			}
		}

		public function get handCards():Vector.<Card>
		{
			return hand.cardsSet;
		}

		public function render(openAngle:int = 150, radius:int = 180):void
		{
			var i:int = 1;
			var card:CardRender;

			var stepAngle:Number = openAngle / (numCards + 1);
			var offsetAngle:Number = (openAngle - 180) / 2; // to center hand card

			for each (card in cardsSetRender)
			{
				if (card != null)
				{
					card.y = radius * Math.sin((offsetAngle + i * stepAngle - openAngle) / 180 * Math.PI);
					card.x = radius * Math.cos((offsetAngle + i * stepAngle - openAngle) / 180 * Math.PI);
					card.rotationZ = (90 + offsetAngle + i * stepAngle - openAngle);

					i++;
				}
			}


			_radius = radius;
			_openAngle = openAngle;
		}

		public function get radius():int
		{
			return _radius;
		}

		public function set radius(value:int):void
		{
			if (_radius != value)
			{
				render(_openAngle, _radius);

				_radius = value;
			}
		}

		public function get openAngle():int
		{
			return _openAngle;
		}

		public function set openAngle(value:int):void
		{
			if (_openAngle != value)
			{
				render(_openAngle, _radius);

				_openAngle = value;
			}
		}
	}
}